<?php

require_once 'Base.php';

class Query extends Base {
    public function send(){
        if($_POST){
            $requestParams=$_POST;

            $requestParams['sign']=$this->generateSign($requestParams);
            $url="https://api.mch.weixin.qq.com/applyment/micro/getstate";

            $response=$this->curlRequest($url,$this->arrayToXml($requestParams),[],true);

            if($response['return_code'] != 'SUCCESS'){
                exit($response['return_msg']);
            }
            if($response['result_code'] != 'SUCCESS'){
                exit($response['err_code_des']);
            }
            $this->checkSign($response);

            $res='商户申请单号为：'.$response['applyment_id'].'<br/>'
                .'申请状态：'.$response['applyment_state'].'<br/>'
                .'申请状态描述：'.$response['applyment_state_desc'].'<br/>';
            if($response['applyment_state']=='TO_BE_SIGNED'||$response['applyment_state']=='FINISH'){
                $res.='小微商户号：'.$response['sub_mch_id'].'<br/>'
                    .'签约链接：'.$response['sign_url'].'<br/>';
            }
            if($response['applyment_state']=='REJECTED'){
                $res.='审核详情：'.$response['audit_detail'];
            }
            //将结果进行储存
            file_put_contents($this->queryDataPath,json_encode($response),FILE_APPEND);
            echo $res;
        }
    }
}

return (new Query())->send();